package com.mylib.libcore.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

import com.mylib.libcore.R;
import com.mylib.widget.ScrollTextView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 *TimeUtils
 */
public class TimeUtils {

    /**
     *  时间转文字
     * @return
     */
    public static String timeStringToTimeString(String time, String format1, String format2) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format1);
            Date date = null;
            date = formatter.parse(time);
            return new SimpleDateFormat(format2).format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 把毫秒时长转换为类似03:50的String*/
    public static String durationToString(int duration){
        int duration_second = duration / 1000;//单位转换为秒
        int minute = duration_second / 60;//求得分钟数
        int second = duration_second % 60;//求得不满一分钟的秒数
        StringBuilder sb = new StringBuilder();
        if(minute < 10)
            sb.append(0);
        sb.append(minute);
        sb.append(':');
        if(second < 10)
            sb.append(0);
        sb.append(second);
        return sb.toString();
    }

    /**
     * @param time
     * @return
     */
    public static String data(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            String stf = String.valueOf(l);
            times = stf.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    //返回的就是10位的时间戳
    public static String getNowTime() {
        long time = System.currentTimeMillis() / 1000;//获取系统时间的10位的时间戳
        String str = String.valueOf(time);
        return str;
    }
    //日期转换为星期
    public static String dateToWeek(String datetime) {
        if(datetime == null || datetime.length()<10)return "";
        if(datetime.length()>10)datetime = datetime.substring(0,10);
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        try{
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Calendar cal = Calendar.getInstance(); // 获得一个日历
            Date datet = null;
            try {
                datet = f.parse(datetime);
                cal.setTime(datet);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
            if (w < 0) w = 0;
            return weekDays[w];
        }catch (Exception e){
            return "";
        }
    }

    /**
     * 日期转换成秒数
     */
    public static long timeToMiao(String expireDate) {
        if (expireDate == null || expireDate.trim().equals(""))
            return 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(expireDate);
            return (long)date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * 更具日期获取星期几
     */
    public static String getWeekByTimeString(String pTime){
        String week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(pTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            week += "星期天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            week += "星期一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            week += "星期二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            week += "星期三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            week += "星期四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            week += "星期五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            week += "星期六";
        }
        return week;
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getTime() {
        Date date = new Date();// 创建一个时间对象，获取到当前的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");// 设置时间显示格式
        String format = sdf.format(date);
        return format;
    }

    /**
     * long转分秒
     *
     * @return
     */
    public static String getTextTime(Long tiem) {
        if(UIUtils.isEmpty(tiem)){
            return "00:00";
        }else {
            String miu = (tiem/1000)/60+"";
            Log.d("djs_time","millisUntilFinished=1="+miu);
            if(miu.length()<2){
              miu = "0"+miu;
            }
            String scend = (tiem/1000)%60+"";
            Log.d("djs_time","millisUntilFinished=2="+scend);
            if(scend.length()<2){
                scend = "0"+scend;
            }
            return miu+":"+scend;
        }

    }

    /**
     * Date转年月日
     *
     * @return
     */
    public static String getDateTime(Date date) {
        if(UIUtils.isEmpty(date)){
            return "";
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 设置时间显示格式
            String format = sdf.format(date);
            return format;
        }
    }

    /**
     * Date转年月日
     *
     * @return
     */
    public static String getDateTimeHour(Date date) {
        if(UIUtils.isEmpty(date)){
            return "";
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");// 设置时间显示格式
            String format = sdf.format(date);
            return format;
        }
    }

    /**
     * Date转年月日
     *
     * @return
     */
    public static String getCusDateTime(String date) {
        if(UIUtils.isEmpty(date)){
            return "";
        }else {
            String time="";
            if(date.length()>7){
                time = date.substring(0,4)+"年"+date.substring(5,7)+"月";
            }
            return time;
        }

    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) {
        try {
            if(UIUtils.isEmpty(s)){
                return "";
            }
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            return res;
        }catch (Exception e){
            return "";
        }
    }
    /*
     * 将时间转换为时间戳
     */
    public static String dateToStampTwo(String s){
       try {
           String res;
           SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
           Date date = simpleDateFormat.parse(s);
           long ts = date.getTime();
           res = String.valueOf(ts);
           return res;
       }catch (Exception e){
           return System.currentTimeMillis()+"";
       }
    }
    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        if(UIUtils.isEmpty(s)){
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为闹钟时间
     */
    public static String stampToDateClock(String s){
        if(UIUtils.isEmpty(s)){
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDateYear(String s){
        if(UIUtils.isEmpty(s)){
            return "";
        }
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 更具当前时间-更换首页背景-和通知图片
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    public static void customHomeBgByTime(Context context, LinearLayout layout ){
        Calendar calendars = Calendar.getInstance();
        int hour = calendars.get(Calendar.HOUR_OF_DAY);
        int minute = calendars.get(Calendar.MINUTE);
        int apm = calendars.get(Calendar.AM_PM);

        //早上8:30--1:00  中午1:00--5:30 傍晚:5:30--9:30 夜晚:9:30--8:30
        int now_minute = hour*60+minute;//当前分钟
        if(now_minute>=(4*60+30) && now_minute<13*60){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_1));
        }

        if(now_minute>=13*60 && now_minute<16*60+30){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_2));
        }

        if(now_minute>=16*60+30 && now_minute<22*60+30){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_3));
        }

        if(now_minute>=22*60+30 || now_minute<(4*60+30)){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_4));
        }

    }


    /**
     * 更具当前时间-更换首页背景-和通知图片
     */
    @SuppressLint("UseCompatLoadingForDrawables")
    public static void customHomeBgByTimeRl(Context context, RelativeLayout layout ){
        Calendar calendars = Calendar.getInstance();
        int hour = calendars.get(Calendar.HOUR_OF_DAY);
        int minute = calendars.get(Calendar.MINUTE);
        int apm = calendars.get(Calendar.AM_PM);
        //早上8:30--1:00  中午1:00--5:30 傍晚:5:30--9:30 夜晚:9:30--8:30
        int now_minute = hour*60+minute;//当前分钟
        if(now_minute>=(4*60+30) && now_minute<13*60){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_1));
        }

        if(now_minute>=13*60 && now_minute<16*60+30){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_2));
        }

        if(now_minute>=16*60+30 && now_minute<22*60+30){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_3));
        }

        if(now_minute>=22*60+30 || now_minute<(4*60+30)){
            layout.setBackground(context.getResources().getDrawable(R.drawable.img_weather_main_bg_4));
        }

    }
}
